package com.vdf.sisconsolid.rest.definition;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.Consumes;
import javax.ws.rs.FormParam;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.Context;

import com.vdf.sisconsolid.controller.StaffController;
import com.vdf.sisconsolid.rest.type.User;

@Path("security")
public class RestSecutiry {

	@Context HttpServletRequest request;
	
	@POST
	@Path("login")
	@Produces("application/json")
	@Consumes("application/x-www-form-urlencoded")
	public Map<String, Object> login(@FormParam("username") String username, @FormParam("password") String password) {
		if (username == null || password == null) return StaffController.getResponseRest(false, "loggin", "fail loggin", null);
		else {
			User u = new User();
			u.setFirstName("fn");
			u.setLastName("ln");
			u.setUsername(username);
			
			request.getSession().setAttribute("user", u);
			
			return StaffController.getResponseRest(true, "login", "success login", u);
		}
	}
	
	@GET
	@Path("checkLogin")
	@Produces("application/json")
	public Map<String, Object> checkLoggin() {
		User user = (User) request.getSession().getAttribute("user");
		if (user != null){
			return StaffController.getResponseRest(true, "login", "success login", user);
		}else {
			return StaffController.getResponseRest(false, "loggin", "fail loggin", null);
		}
	}
}
